/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sqltest;

import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
/**
 *
 * @author Adam
 */
public class SQLTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       Connection conn =null;
        try {
            // Step 1: Load the JDBC driver. 
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            System.out.println("?");
            // Step 2: Establish the connection to the database. 
            System.out.println("My IP: " + InetAddress.getLocalHost().getHostAddress());
            String url = "jdbc:mysql://localhost:3306";
            System.out.println();
            System.out.println("The URL is: " + url);
            conn = DriverManager.getConnection(url, "root", "ftcp1234");
            Statement s = conn.createStatement();
            s.executeUpdate("USE mydb");
            s.executeUpdate("DROP TABLE IF EXISTS animal");
            s.executeUpdate(
                    "CREATE TABLE animal ("
                    + "id INT UNSIGNED NOT NULL AUTO_INCREMENT,"
                    + "PRIMARY KEY (id),"
                    + "name CHAR(40), category CHAR(40))");  
            s.executeUpdate("INSERT INTO animal(name, category) VALUES " 
                    +  "('snake', 'reptile')");
            s.close();
            System.out.println("Updated!");
        } catch (Exception e) {
            System.err.println("Got an exception! ");           
            System.err.println(e.getMessage());
           
           
        }
        finally
           {
               if (conn != null)
               {
                   try
                   {
                       conn.close ();
                       System.out.println ("Database connection terminated");
                   }
                   catch (Exception e) { /* ignore close errors */ }
               }
    }
    }
}
